﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Text.RegularExpressions;

public class HttpConnect
{

    private static HttpConnect _instance;
    public static HttpConnect Instance
    {
        get
        {
            if (_instance == null)
            {
                _instance = new HttpConnect();
            }
            return _instance;
        }
    }

    public WWW HttpRequestAli(string url)
    {


        WWW www = new WWW(url);
        return www;
    }

    public WWW HttpRequest(string url, WWWForm form)
    {
        EginTools.Log(url);

        if (form == null) { form = new WWWForm(); }

        if (form != null)
        {
            long ms = EginTools.nowMinis();
            long mms = ms + EginTools.localBeiJingTime;
            string ccode = EginTools.encrypTime(mms.ToString());
            form.AddField("client_code", ccode);
        }


        WWW www = (form == null) ? new WWW(url) : new WWW(url, form);
        return www;
    }

    IEnumerator SendGet(string _url)
    {
        WWW getData = new WWW(_url);
        yield return getData;
        if (getData.error != null)
        {
            Debug.Log(getData.error);
        }
        else
        {
            Debug.Log(getData.text);
        }
    }
    IEnumerator SendPost(string _url, WWWForm _wForm)
    {
        WWW postData = new WWW(_url, _wForm);
        yield return postData;
        if (postData.error != null)
        {
            Debug.Log(postData.error);
        }
        else
        {
            Debug.Log(postData.text);
        }
    }

    public void TestHttpSend()
    {
        //测试GET方法  
        SendGet("http://kun.show.ghostry.cn/?int=5");

        //测试POST方法  
        WWWForm form = new WWWForm();
        form.AddField("int", "6");
        SendPost("http://kun.show.ghostry.cn/", form);
    }
}